package com.framework.modules.common.service.impl;

import com.framework.common.config.SystemConfigName;
import com.framework.common.utils.DateUtils;
import com.framework.common.utils.PayDigestUtil;
import com.framework.common.utils.QuickHelper;
import com.framework.modules.base.entity.UserOrderEntity;
import com.framework.modules.common.channel.other.OtherPayConfig;
import com.framework.modules.common.service.IPayChannel4OtherService;
import com.squareup.okhttp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付渠道接口:支付宝
 * @author: xfy
 * @date: 2020/10/28
 */
@Service
public class PayChannel4OtherServiceImpl implements IPayChannel4OtherService {

    private static final Logger _log = LoggerFactory.getLogger(PayChannel4OtherServiceImpl.class);

    @Autowired
    private OtherPayConfig otherPayConfig;

    @Override
    public Object doOtherPayWapReq(UserOrderEntity userOrderEntity)  {
        _log.info("###### 支付下单处理开始 ######");
        String payOrderid=userOrderEntity.getId();
        otherPayConfig.init(QuickHelper.getConfigString(SystemConfigName.PAYMENT_SDK_OTHERPAY));
        String payMemberid=otherPayConfig.getApp_id();
        String payApplydate= DateUtils.format(new Date(),"yyyy-MM-dd hh:mm:ss");
        String payBankcode=otherPayConfig.getChannel_id();
        String payNotifyurl=otherPayConfig.getNotify_url();
        String payCallbackurl=otherPayConfig.getReturn_url();
        BigDecimal payAmount=userOrderEntity.getAmount();
        String payProductname=userOrderEntity.getSubject();

        Map<String,Object> para=new HashMap<>();
        para.put("pay_orderid",payOrderid);
        para.put("pay_memberid",payMemberid);
        para.put("pay_applydate",payApplydate);
        para.put("pay_bankcode",payBankcode);
        para.put("pay_notifyurl",payNotifyurl);
        para.put("pay_callbackurl",payCallbackurl);
        para.put("pay_amount",payAmount);
        String payMd5sign=PayDigestUtil.getSign(para,otherPayConfig.getKey()).toUpperCase();
        para.put("pay_md5sign",payMd5sign);
        para.put("pay_productname",payProductname);

        _log.info(String.format("other支付请求参数：%s", QuickHelper.object2json(para)));
        try {
            OkHttpClient httpClient=new OkHttpClient();
            RequestBody requestBody=new FormEncodingBuilder().add("pay_orderid",payOrderid)
                    .add("pay_productname",payProductname).add("pay_memberid",payMemberid)
                    .add("pay_applydate",payApplydate).add("pay_bankcode",payBankcode)
                    .add("pay_notifyurl",payNotifyurl).add("pay_callbackurl",payCallbackurl)
                    .add("pay_amount",payAmount.toString()).add("pay_md5sign",payMd5sign)
                    .build();
            Request request = new Request.Builder().url(otherPayConfig.getUrl())
                    .addHeader("Content-Type", "application/x-www-form-urlencoded").post(requestBody)
                    .build();
            Response res = httpClient.newCall(request).execute();
            String payurl = res.body().string();
            _log.info("返回参数："+payurl);
            _log.info("###### 支付下单处理完成 ######");
            Map<String,Object> result=new HashMap<>();
            result.put("payurl",payurl);
            result.put("payOrderid",payOrderid);
            return result;
        }catch (IOException e){
            e.printStackTrace();
            return "获取失败";
        }
    }

}
